package main

import (
	"strconv"
	"strings"
)

func subdomainVisits(cpdomains []string) []string {

	var cpdomainMap map[string]int

	cpdomainMap = make(map[string]int)

	for _, v := range cpdomains {
		arr1 := strings.Split(v, " ")

		count, _ := strconv.Atoi(arr1[0])
		arr2 := strings.Split(arr1[1], ".")

		for i := 0; i < len(arr2); i++ {
			cpdomainMap[strings.Join(arr2[i:len(arr2)], ".")] += count
		}
	}

	ans := []string{}

	for k, v := range cpdomainMap {
		ans = append(ans, strconv.Itoa(v)+" "+k)
	}

	return ans
}
